import { Component, ElementRef, OnInit, Output, Input, ViewChild, AfterViewInit, EventEmitter} from '@angular/core';
import { Service } from '../../../server/server';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
declare var $:any;

@Component({
  selector: 'app-database-table',
  templateUrl: './database-table.component.html'
})
export class DatabaseTableComponent implements OnInit {

  @Input() tableNumber : number;
  right: any = { "system" : {}, "database" : {} };
  constructor(private tableListService: CudTableService,private removeTableService: RemoveTableService,private server: Service) { 
    this.right = $.extend(this.right, server.getRight());
  }

  ngOnInit() {
    // this.tabVal = [
    //   {
    //     table_id:'001',
    //     table_name:'dh_resource_categorys',
    //     table_desc:'资源分类表' 
    //   },
    //   {
    //     table_id:'002',
    //     table_name:'dh_resources',
    //     table_desc:'资源表' 
    //   },
    //   {
    //     table_id:'003',
    //     table_name:'dh_resource_type',
    //     table_desc:'资源类型表' 
    //   },  
    //   {
    //     table_id:'004',
    //     table_name:'dh_resource_relations',
    //     table_desc:'资源关系表' 
    //   }, 
    //   {
    //     table_id:'005',
    //     table_name:'dh_resource_category_relations',
    //     table_desc:'资源_分类关系表' 
    //   }, 
    //   {
    //     table_id:'006',
    //     table_name:'dh_article_contents',
    //     table_desc:'文章内容表' 
    //   },
    //   {
    //     table_id:'007',
    //     table_name:'dh_articles',
    //     table_desc:'文章表' 
    //   },
    // ]

    this.getTabList(1,20,'');

  }

  @Output()
  tabeListOutput: EventEmitter<string> = new EventEmitter();

  public tabVal: any;
  getTabList(pageNumber, pageSize, tableName) {
    let url = this.server.baseUrl + 'Service/Config/getBackUpDBList';
    let data = { pageNumber: pageNumber, pageSize: pageSize, queryCriteria:tableName}
    this.tableListService.postRequest(url, data).then((data: any) => {
      this.tabVal = data.result;
      this.tabeListOutput.emit(data.length);
    }).catch();
  }

  //详情
  details(tableId,val) {
    let url = this.server.baseUrl + 'Service/TalbesService/getTablesColumnList';
    let data = { tableId: tableId };
    this.tableListService.postRequest(url, data).then((res: any) => {
      if(val == 2){
        this.server.databaseupdateeventEmit.emit(res.result);
      }else{
        this.server.databasedetailseventEmit.emit(res.result);
      }
    })
    
  }

  // 全选
  public isSelectAll: boolean = false;
  public isChose: boolean = false;
  public isSelect: boolean = true;
  public isDisable: any;              // 按钮（启用/禁用）

  @Output() isSelectFromTable = new EventEmitter<boolean>();

  selectAll(bool, tabVal) {
    this.isSelectAll = this.isChose = this.removeTableService.removeAll(bool, tabVal, 'id');
    this.isSelect = !this.isSelectAll;
    this.isSelectFromTable.emit(this.isSelect);
  }

  // 单选（多选）
  chose(bool, tableId, length) {
    this.isDisable = this.removeTableService.removeChose(bool, tableId, length);
    this.isSelectAll = this.isDisable.bool;
    this.isDisable.len ? this.isSelect = false : this.isSelect = true;
    this.isSelectFromTable.emit(this.isSelect);
  }

  // 没有数据时取消勾选全选按钮
  cancelSelectAll() {
    this.isSelectAll = false;
    this.isChose = false;
  }

}
